/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package command;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

import config.Debug;
import config.Helper;
import config.Settings;

/**
 * 
 * @author Yang Wang <yang@digitalmenubox.com>
 */
public class CommandParser {

	private Map<String, Command> commandMap = new HashMap<String, Command>();

	public CommandParser() {
		super();
		if (Helper.getPlatform() == Helper.LINUX_OS) {
			// store linux commands here
			commandMap.put("setVolume", new command.linux.setVolume());
			commandMap.put("restartPlayer", new command.linux.restartPlayer());
			commandMap.put("setTransferType", new command.linux.setTransferType());
			commandMap.put("clearPlayer", new command.linux.clearPlayer());
			commandMap.put("rebootSystem", new command.linux.rebootSystem());
		} else if (Helper.getPlatform() == Helper.MAC_OS) {
			// store linux commands here
			commandMap.put("setVolume", new command.mac.setVolume());
			commandMap.put("restartPlayer", new command.mac.restartPlayer());
			commandMap.put("setTransferType", new command.mac.setTransferType());
			commandMap.put("clearPlayer", new command.mac.clearPlayer());
			commandMap.put("rebootSystem", new command.mac.rebootSystem());
		}
	}

	public Set<String> getCommands() {
		return commandMap.keySet();
	}

	public void executeAllCommands() {
		List<String> commandsInFile = new ArrayList<String>();
		try {
			File commandFile = new File(Settings.getScriptDirectory(), "commands.txt");

			if (!commandFile.exists()) {
				return;
			}
			// read comands into player and delete the file before the command
			// ran
			Scanner fileScanner = new Scanner(commandFile);
			Set<String> commands = this.getCommands();
			while (fileScanner.hasNextLine()) {
				commandsInFile.add(fileScanner.nextLine());
			}
			fileScanner.close();
			commandFile.delete();

			// run the commands
			for (String line : commandsInFile) {
				String[] parts = line.split(" ");
				List<String> argsList = Arrays.asList(parts);
				argsList = argsList.subList(1, argsList.size());
				String[] commandArgs = argsList.toArray(new String[] {});
				for (String command : commands) {
					if (command.equals(parts[0])) {
						try {
							this.commandMap.get(command).execute(commandArgs);
						} catch (Exception e) {
							Debug.log("Error while running command: " + command + ": " + e.getMessage(), Debug.ERROR_LEVEL);
						}
						Debug.log("Executing command: " + line, Debug.SYSTEM_LEVEL);
					}
				}
			}
		} catch (FileNotFoundException ex) {
			Debug.log(ex.getMessage(), Debug.EXCEPTION_LEVEL);
		}
	}

	public static void main(String[] args) throws IOException {
		CommandParser parser = new CommandParser();
		parser.executeAllCommands();
	}
}
